package com.nwork.cateye.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nwork.cateye.CityListActivity;
import com.nwork.cateye.R;
import com.nwork.cateye.adapter.MovieFragmentAdapter;

import java.util.LinkedList;
import java.util.List;

/**
 * MovieFragment
 */
public class MovieFragment extends Fragment implements ViewPager.OnPageChangeListener, View.OnClickListener {

    private LinearLayout scrollLin;
    private ViewPager movieViewpager;
    private ImageView scrollImg;
    private int currentIndex;
    private int width;
    private ImageView cityListImg;
    private TextView cityListTxt;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_movie, container, false);
        initCom(view);
        scrollLin.measure(0, 0);
        width = scrollLin.getMeasuredWidth();
        movieViewpager.setOnPageChangeListener(this);
        List<Fragment> fragments = new LinkedList<>();
        MovieJiFragment movieJiFragment = new MovieJiFragment();
        MovieZhengFragment movieZhengFragment = new MovieZhengFragment();

        fragments.add(movieZhengFragment);
        fragments.add(movieJiFragment);
        movieViewpager.setAdapter(new MovieFragmentAdapter(getChildFragmentManager()
                , fragments));
        cityListImg.setOnClickListener(this);
        cityListTxt.setOnClickListener(this);
        return view;
    }

    private void initCom(View view) {
        movieViewpager = (ViewPager) view.findViewById(R.id.movie_viewpager);
        scrollLin = (LinearLayout) view.findViewById(R.id.radio_group_layout);
        scrollImg = (ImageView) view.findViewById(R.id.scroll_img);
        cityListImg = (ImageView) view.findViewById(R.id.city_list_img);
        cityListTxt = (TextView) view.findViewById(R.id.city_list_txt);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) scrollImg
                .getLayoutParams();
        if (currentIndex == 0 && position == 0) {
            lp.leftMargin = (int) (positionOffset * (width * 1.0 / 2) + currentIndex
                    * (width / 2));
        } else if (currentIndex == 1 && position == 0) {
            lp.leftMargin = (int) (-(1 - positionOffset)
                    * (width * 1.0 / 2) + currentIndex
                    * (width / 2));
        }
        scrollImg.setLayoutParams(lp);
    }

    @Override
    public void onPageSelected(int position) {
        currentIndex = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.city_list_img:
            case R.id.city_list_txt:
                Intent intent = new Intent(getActivity(), CityListActivity.class);
                startActivity(intent);
                break;
        }
    }
}
